

package org.liyufeng.project.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.liyufeng.project.model.PayrollDepartment;
import org.liyufeng.project.dao.PayrollDepartmentDao;
import org.liyufeng.project.dao.impl.PayrollDepartmentDaoImpl;
import org.liyufeng.project.pojo.Token;
import org.liyufeng.project.pojo.WeixinUserInfo;
import org.liyufeng.project.addresslist.UserManagement;
import org.liyufeng.project.util.CommonUtil;
import org.liyufeng.project.util.ParamesAPI;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PayrollDepartmentServlet extends HttpServlet {
	private static final long serialVersionUID = 6790029477091312226L;
	private static Logger log = LoggerFactory.getLogger(PayrollServlet.class);

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setHeader("pragma", "no-cache");
		response.setHeader("cache-control", "no-cache");

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		
		PrintWriter out = response.getWriter();

		String method = request.getParameter("method");
		String UserId = request.getParameter("UserID");
		//按照微信UserID查询用户信息
		Token token = CommonUtil.getToken(ParamesAPI.corpId, ParamesAPI.corpsecret);
		WeixinUserInfo UserInfo=UserManagement.GetPerson(token.getAccessToken(),UserId);
		//解析出用户所在部门
		String department="";
		Object personDepartment=UserInfo.getDepartment();
		if(personDepartment.toString().equals("[3]")){
			department="水场车间";
		}else if(personDepartment.toString().equals("[5]")){
			department="片削染车间";
		}else if(personDepartment.toString().equals("[6]")){
			department="整理车间";
		}
		
		
		PayrollDepartmentDao payrollDepartmentDao = new PayrollDepartmentDaoImpl(); // DAO
		log.info("UserId:{}",UserId);
		log.info("Department:{}",department);
		
		if (method.equals("getAll")) {
			ArrayList<PayrollDepartment> payrollDepartment = payrollDepartmentDao.getAll(department);
			String jsonPayrollDepartment = JSONArray.fromObject(payrollDepartment).toString();
			out.write(jsonPayrollDepartment);
		} 
		out.flush();
		out.close();
	}
}